var MaiTong_Touch = function(){
	var noop = function(){};
	
	var use = function(){
		var trim = function(s){
			return s.replace(/^\s+|\s+$/g, '');
		};
		
		var loadScript = function(src, callback){
			var script = document.createElement('script');
			script.type = 'text/javascript';
			script.async = script.defer = true;
			var addReadyHandleFlag = false;
			
			var stateChange = function(){
				if(script.readyState === 'complete' || script.readyState === 'loaded'){
					loadHandle.call(script);
				}
			};
			
			var loadHandle = function(){
				script.onload = script.onerror = noop;
				if(addReadyHandleFlag){
					script.detachEvent('onreadystatechange', stateChange);
				}
				
				setTimeout(callback, 0);
			};
			
			script.onload = script.onerror = loadHandle;
			
			if(script.readyState && !script.addEventListener){ // ie8--
				addReadyHandleFlag = true;
				script.attachEvent('onreadystatechange', stateChange);
			}
			
			script.src = src;
			
			//document.getElementsByTagName('head')[0].appendChild(script);
			document.body.appendChild(script);
		};
		
		var loader = function(sources, callback){
			var me = arguments.callee;
			
			loadScript(sources.shift(), function(){
				if(sources.length){
					me(sources, callback);
				}else{
					callback();
				}
			});
		};
		
		var use = function(){
			var args = [].slice.call(arguments);
			if(!args.length){
				return;
			}
			
			var callback = noop;
			if(typeof args[args.length - 1] === 'function'){
				callback = args.pop();
			}
			
			var sources = [];
			var src;
			for(var i = 0; i < args.length; i++){
				src = args[i];
				if(typeof src === 'function'){
					src = src();
				}
				
				if(typeof src === 'string'){
					src = trim(src);
					if(src){
						sources.push(src);
					}
				}
			}
			
			if(!sources.length){
				callback();
			}else{
				loader(sources, callback);
			}
		};
		
		return use;
	}.call(this);

	var loadCSS = function(){
		var loaded = false;
		
		return function(){
			if(loaded){
				return;
			}
			
			var link = document.createElement('link');
			link.rel = 'stylesheet';
			link.type = 'text/css';
			link.media = 'all';
			link.href = '{{=root}}dist/css/webim.css?v={{=version}}';
			document.getElementsByTagName('head')[0].appendChild(link);
			
			loaded = true;
		};
	}.call(this);

	var loadJS = function(){
		var loaded = false;
		var loading = false;
		var handles = [];
		
		return function(cb){
			cb = (typeof cb === 'function') ? cb : noop;
			
			if(loaded){
				cb();
				return;
			}else{
				handles.push(cb);
			}
			
			if(loading){
				return;
			}
			
			loading = true;
			
			use('{{=root}}dist/js/webim.js?v={{=version}}', function(){
				loading = false;
				loaded = true;
				
				while(handles.length){
					handles.shift()();
				}
			});
		};
	}.call(this);;

	return {
		_: {
			ready: []
		},
		isNewPage: false,
		isReady: false,
		init: function(){
			var _this = this;
			
			loadCSS();
			loadJS(function(){
				_this.isReady = true;
				_this.checkReady();
			});
		},
		ready: function(fn){
			(typeof fn === 'function') && this._.ready.push(fn);
			
			this.checkReady();
		},
		checkReady: function(){
			if(this.isReady){
				while(this._.ready.length){
					this._.ready.shift().call(this);
				}
			}
		},
		start: function(page, data){
			this.ready(function(){
				var _this = this;
				
				if(!this.api){
					this.api = new this.IMClient();
				}else{
					
				}
				
				//
				if(page === 'chat2' && data){
					this.api.isNewPage = !!this.isNewPage;
					!this.api.isStarted() && this.api.start();
					
					this.api.getUserByDomainUser({domainUserId: data.domainUserId, domainUserName: data.domainUserName}, function(user){
						data = {
							domainUserId: data.domainUserId,
							userName: data.domainUserName,
							uid: user.id,
							pid: data.pid
						};
						
						_this.api.start(page, data);
					});
				}else{
					this.api.start(page, data);
				}
			});
			
			this.init();
		},
		chatTo: function(data){
			this.start('chat2', data);
		},
		contacts: function(){
			this.start('contacts');
		},
		profile: function(data){
			this.start('profile', data);
		}
	};
}.call(this);